<template>
    <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from '../../../../assets/js/charts/resize'
export default {
    mixins: [resize],
    props: {
        chartData: {
            type: Array,
            default: [],
        },
        className: {
            type: String,
            default: 'chart'
        },
        id: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '100%'
        },
    },
    data() {
        return {
            chart: null,
        }
    },
    mounted() {
        this.initChart();
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(document.getElementById(this.id))
            // 使用刚指定的配置项和数据显示图表。
            this.chart.setOption({
                tooltip: {
                    trigger: 'item',
                    formatter: "{b}:{d}%"
                },
                title: {
                    text: '',
                    left: 'center'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                },
                series: [{
                    name: '访问来源',
                    type: 'pie',
                    radius: '50%',
                    data: this.chartData,
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    },
                    color: ['#5470c6','#91cc75','#fac858', '#ee6666','#73c0de','#3ba272','#fc8452','#9a60b4','#ea7ccc'],
                }]
            });
        }
    }
}
</script>
